package net.wanho.ums.servlet;

import com.alibaba.fastjson.JSON;
import net.wanho.ums.entity.User;
import net.wanho.ums.service.UserService;
import net.wanho.ums.service.impl.UserServiceImpl;
import net.wanho.ums.vo.AjaxResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Author：汤小洋
 * Date：2024-12-09 16:55
 * Description：<描述>
 */
@WebServlet("/register")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        int age = Integer.parseInt(req.getParameter("age"));

        User user = new User(null, username, password, age);
        userService.add(user);

        resp.setContentType("application/json;charset=utf8");
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(AjaxResult.success()));
        out.close();
    }
}
